import React, { Component } from "react";
import { connect } from "react-redux";
import { setNameAction, set } from "./store/actionsCreators";
import jsonp from "jsonp";
class Home extends Component {
  search() {
    const url = "http://suggestion.baidu.com/su?wd=";
    jsonp(url + "react", { param: "cb" }, (err, data) => {
      if (err) {
        console.log("123");
      } else {
        console.log(data);
      }
    });
  }
  render() {
    return (
      <div>
        <h1>{this.props.userName}</h1>
        <input type="text" value={this.props.userName} onChange={this.search} />
      </div>
    );
  }
}

// mapStateToProps:reducer里的状态到当前组件的属性上
const mapStateToProps = ({ data }) => {
  // console.log(data);
  return {
    userName: data.name
  };
};
const mapDispatchToProps = dispatch => {
  return {
    // 修改动作的方法
    setNameAction({ target }) {
      dispatch(setNameAction(target.value));
      
    }
  };
};
export default connect(mapStateToProps, mapDispatchToProps)(Home); // 使用连接器
